<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Related objects reference &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Models" href="index.html" />
    <link rel="next" title="Model Meta options" href="options.html" />
    <link rel="prev" title="Model field reference" href="fields.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="fields.html" title="Model field reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="options.html" title="Model &lt;tt class=&#34;docutils literal&#34;&gt;&lt;span class=&#34;pre&#34;&gt;Meta&lt;/span&gt;&lt;/tt&gt; options">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-relations">
            
  <div class="section" id="s-related-objects-reference">
<span id="related-objects-reference"></span><h1>Related objects reference<a class="headerlink" href="#related-objects-reference" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="django.db.models.fields.related.RelatedManager">
<em class="property">class </em><tt class="descname">RelatedManager</tt><a class="headerlink" href="#django.db.models.fields.related.RelatedManager" title="Permalink to this definition">¶</a></dt>
<dd><p>A &#8220;related manager&#8221; is a manager used in a one-to-many or many-to-many
related context. This happens in two cases:</p>
<ul>
<li><p class="first">The &#8220;other side&#8221; of a <a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> relation.
That is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Reporter</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">reporter</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Reporter</span><span class="p">)</span>
</pre></div>
</div>
<p>In the above example, the methods below will be available on
the manager <tt class="docutils literal"><span class="pre">reporter.article_set</span></tt>.</p>
</li>
<li><p class="first">Both sides of a <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> relation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Topping</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">Pizza</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">toppings</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Topping</span><span class="p">)</span>
</pre></div>
</div>
<p>In this example, the methods below will be available both on
<tt class="docutils literal"><span class="pre">topping.pizza_set</span></tt> and on <tt class="docutils literal"><span class="pre">pizza.toppings</span></tt>.</p>
</li>
</ul>
<dl class="method">
<dt id="django.db.models.fields.related.RelatedManager.add">
<tt class="descname">add</tt>(<em>obj1</em><span class="optional">[</span>, <em>obj2</em>, <em>...</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.fields.related.RelatedManager.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified model objects to the related object set.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">entry_set</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="c"># Associates Entry e with Blog b.</span>
</pre></div>
</div>
<p>In the example above, in the case of a
<a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> relationship,
<tt class="docutils literal"><span class="pre">e.save()</span></tt> is called by the related manager to perform the update.
Using <tt class="docutils literal"><span class="pre">add()</span></tt> with a many-to-many relationship, however, will not
call any <tt class="docutils literal"><span class="pre">save()</span></tt> methods, but rather create the relationships
using <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.bulk_create" title="django.db.models.query.QuerySet.bulk_create"><tt class="xref py py-meth docutils literal"><span class="pre">QuerySet.bulk_create()</span></tt></a>. If you need to execute
some custom logic when a relationship is created, listen to the
<a class="reference internal" href="../signals.html#django.db.models.signals.m2m_changed" title="django.db.models.signals.m2m_changed"><tt class="xref py py-data docutils literal"><span class="pre">m2m_changed</span></tt></a> signal.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.fields.related.RelatedManager.create">
<tt class="descname">create</tt>(<em>**kwargs</em>)<a class="headerlink" href="#django.db.models.fields.related.RelatedManager.create" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new object, saves it and puts it in the related object set.
Returns the newly created object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">b</span><span class="o">.</span><span class="n">entry_set</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">headline</span><span class="o">=</span><span class="s">&#39;Hello&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">body_text</span><span class="o">=</span><span class="s">&#39;Hi&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">pub_date</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span><span class="p">)</span>

<span class="go"># No need to call e.save() at this point -- it&#39;s already been saved.</span>
</pre></div>
</div>
<p>This is equivalent to (but much simpler than):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">Entry</span><span class="p">(</span>
<span class="gp">... </span>    <span class="n">blog</span><span class="o">=</span><span class="n">b</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">headline</span><span class="o">=</span><span class="s">&#39;Hello&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">body_text</span><span class="o">=</span><span class="s">&#39;Hi&#39;</span><span class="p">,</span>
<span class="gp">... </span>    <span class="n">pub_date</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2005</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that there&#8217;s no need to specify the keyword argument of the model
that defines the relationship. In the above example, we don&#8217;t pass the
parameter <tt class="docutils literal"><span class="pre">blog</span></tt> to <tt class="docutils literal"><span class="pre">create()</span></tt>. Django figures out that the new
<tt class="docutils literal"><span class="pre">Entry</span></tt> object&#8217;s <tt class="docutils literal"><span class="pre">blog</span></tt> field should be set to <tt class="docutils literal"><span class="pre">b</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.fields.related.RelatedManager.remove">
<tt class="descname">remove</tt>(<em>obj1</em><span class="optional">[</span>, <em>obj2</em>, <em>...</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.fields.related.RelatedManager.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified model objects from the related object set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span> <span class="o">=</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">entry_set</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="c"># Disassociates Entry e from Blog b.</span>
</pre></div>
</div>
<p>Similar to <a class="reference internal" href="#django.db.models.fields.related.RelatedManager.add" title="django.db.models.fields.related.RelatedManager.add"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a>, <tt class="docutils literal"><span class="pre">e.save()</span></tt> is called in the example above
to perform the update. Using <tt class="docutils literal"><span class="pre">remove()</span></tt> with a many-to-many
relationship, however, will delete the relationships using
<a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.delete" title="django.db.models.query.QuerySet.delete"><tt class="xref py py-meth docutils literal"><span class="pre">QuerySet.delete()</span></tt></a> which
means no model <tt class="docutils literal"><span class="pre">save()</span></tt> methods are called; listen to the
<a class="reference internal" href="../signals.html#django.db.models.signals.m2m_changed" title="django.db.models.signals.m2m_changed"><tt class="xref py py-data docutils literal"><span class="pre">m2m_changed</span></tt></a> signal if you wish to
execute custom code when a relationship is deleted.</p>
<p>For <a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> objects, this method only
exists if <tt class="docutils literal"><span class="pre">null=True</span></tt>. If the related field can&#8217;t be set to <tt class="docutils literal"><span class="pre">None</span></tt>
(<tt class="docutils literal"><span class="pre">NULL</span></tt>), then an object can&#8217;t be removed from a relation without
being added to another. In the above example, removing <tt class="docutils literal"><span class="pre">e</span></tt> from
<tt class="docutils literal"><span class="pre">b.entry_set()</span></tt> is equivalent to doing <tt class="docutils literal"><span class="pre">e.blog</span> <span class="pre">=</span> <span class="pre">None</span></tt>, and because
the <tt class="docutils literal"><span class="pre">blog</span></tt> <a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> doesn&#8217;t have
<tt class="docutils literal"><span class="pre">null=True</span></tt>, this is invalid.</p>
<p>For <a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> objects, this method accepts
a <tt class="docutils literal"><span class="pre">bulk</span></tt> argument to control how to perform the operation.
If <tt class="docutils literal"><span class="pre">True</span></tt> (the default), <tt class="docutils literal"><span class="pre">QuerySet.update()</span></tt> is used.
If <tt class="docutils literal"><span class="pre">bulk=False</span></tt>, the <tt class="docutils literal"><span class="pre">save()</span></tt> method of each individual model
instance is called instead. This triggers the
<a class="reference internal" href="../signals.html#django.db.models.signals.pre_save" title="django.db.models.signals.pre_save"><tt class="xref py py-data docutils literal"><span class="pre">pre_save</span></tt></a> and
<a class="reference internal" href="../signals.html#django.db.models.signals.post_save" title="django.db.models.signals.post_save"><tt class="xref py py-data docutils literal"><span class="pre">post_save</span></tt></a> signals and comes at the
expense of performance.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.fields.related.RelatedManager.clear">
<tt class="descname">clear</tt>()<a class="headerlink" href="#django.db.models.fields.related.RelatedManager.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes all objects from the related object set:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Blog</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">entry_set</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
</pre></div>
</div>
<p>Note this doesn&#8217;t delete the related objects &#8211; it just disassociates
them.</p>
<p>Just like <tt class="docutils literal"><span class="pre">remove()</span></tt>, <tt class="docutils literal"><span class="pre">clear()</span></tt> is only available on
<a class="reference internal" href="fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>s where <tt class="docutils literal"><span class="pre">null=True</span></tt> and it also
accepts the <tt class="docutils literal"><span class="pre">bulk</span></tt> keyword argument.</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Note that <tt class="docutils literal"><span class="pre">add()</span></tt>, <tt class="docutils literal"><span class="pre">create()</span></tt>, <tt class="docutils literal"><span class="pre">remove()</span></tt>, and <tt class="docutils literal"><span class="pre">clear()</span></tt> all
apply database changes immediately for all types of related fields. In
other words, there is no need to call <tt class="docutils literal"><span class="pre">save()</span></tt> on either end of the
relationship.</p>
<p class="last">Also, if you are using <a class="reference internal" href="../../topics/db/models.html#intermediary-manytomany"><em>an intermediate model</em></a> for a many-to-many relationship, some of the
related manager&#8217;s methods are disabled.</p>
</div>
</dd></dl>

<div class="section" id="s-direct-assignment">
<span id="s-id1"></span><span id="direct-assignment"></span><span id="id1"></span><h2>Direct Assignment<a class="headerlink" href="#direct-assignment" title="Permalink to this headline">¶</a></h2>
<p>A related object set can be replaced in bulk with one operation by assigning a
new iterable of objects to it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">new_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">obj1</span><span class="p">,</span> <span class="n">obj2</span><span class="p">,</span> <span class="n">obj3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">e</span><span class="o">.</span><span class="n">related_set</span> <span class="o">=</span> <span class="n">new_list</span>
</pre></div>
</div>
<p>If the foreign key relationship has <tt class="docutils literal"><span class="pre">null=True</span></tt>, then the related manager
will first call <tt class="docutils literal"><span class="pre">clear()</span></tt> to disassociate any existing objects in the related
set before adding the contents of <tt class="docutils literal"><span class="pre">new_list</span></tt>. Otherwise the objects in
<tt class="docutils literal"><span class="pre">new_list</span></tt> will be added to the existing related object set.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Related objects reference</a><ul>
<li><a class="reference internal" href="#direct-assignment">Direct Assignment</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="fields.html">Model field reference</a></li>
    
    
      <li>Next: <a href="options.html">Model <tt class="docutils literal"><span class="pre">Meta</span></tt> options</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html">Models</a>
        
        <ul><li>Related objects reference</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/ref/models/relations.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="fields.html" title="Model field reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="options.html" title="Model &lt;tt class=&#34;docutils literal&#34;&gt;&lt;span class=&#34;pre&#34;&gt;Meta&lt;/span&gt;&lt;/tt&gt; options">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>